#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @Project:     server
# @File:        wiki_api.py
# @Author:      Fan GuiChuan
# @Date:        2025/9/2 14:30
# @Version:     Python3.7
# @Description:
from app.services.wiki_serv import WikiServ
from app.utils.response import success_response, error_response

class WikiApi:
    def __init__(self, logger, blueprint):
        self.logger = logger
        self.wiki_serv = WikiServ(self.logger)
        self.blueprint = blueprint
        self.register_blueprint()

    def register_blueprint(self):
        self.blueprint.add_url_rule('/add_wiki', view_func=self.add_wiki, methods=['POST'])
        self.blueprint.add_url_rule('/get_wiki_tree', view_func=self.get_wiki_tree, methods=['POST'])
        self.blueprint.add_url_rule('get_wiki_content', view_func=self.get_wiki_content, methods=['POST'])
        self.blueprint.add_url_rule('update_wiki_content', view_func=self.update_wiki_content, methods=['POST'])
        self.blueprint.add_url_rule('delete_wiki', view_func=self.delete_wiki, methods=['POST'])
      
    def add_wiki(self):
        """
        新增Wiki
        """
        try:
            ret, data = self.wiki_serv.add_wiki()
            if ret:
                return success_response(data)
            return error_response(data)
        except Exception as e:
            self.logger.exception(e)
            return error_response(str(e))
    
    def get_wiki_tree(self):
        """
        获取Wiki树
        """
        try:
            ret, data = self.wiki_serv.get_wiki_tree()
            if ret:
                return success_response(data)
            return error_response(data)
        except Exception as e:
            self.logger.exception(str(e))
            return error_response(str(e))

    def get_wiki_content(self):
        """
        获取Wiki内容
        """
        try:
            ret, data = self.wiki_serv.get_wiki_content()
            if ret:
                return success_response(data)
            return error_response(data)
        except Exception as e:
            self.logger.exception(str(e))
            return error_response(str(e))

    def update_wiki_content(self):
        """
        更新Wiki文档内容
        """
        try:
            ret, data = self.wiki_serv.update_wiki_content()
            if ret:
                return success_response(data)
            return error_response(data)
        except Exception as e:
            self.logger.exception(str(e))
            return error_response(str(e))

    def delete_wiki(self):
        """
        删除Wiki
        """
        try:
            ret, data = self.wiki_serv.delete_wiki()
            if ret:
                return success_response(data)
            return error_response(data)
        except Exception as e:
            self.logger.exception(str(e))
            return error_response(str(e))
